//
//  WJAspectsController.m
//  JerryIos
//
//  Created by 王军 on 2018/2/22.
//  Copyright © 2018年 王军. All rights reserved.
//

#import "WJAspectsController.h"
#import "WJAspectsDemoController.h"
#import "Aspects.h"

@interface WJAspectsController ()

@end

@implementation WJAspectsController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (IBAction)openCustomController:(id)sender {
    WJAspectsDemoController *demoController = [[WJAspectsDemoController alloc] init];
    [self.navigationController pushViewController:demoController animated:YES];
    
    [demoController aspect_hookSelector:@selector(btnClick:)
                            withOptions:0
                             usingBlock:^(id info, id sender) {
                                        NSLog(@"按钮被点击：%@", sender);
                                    }
                                  error:NULL];
    
    [demoController aspect_hookSelector:@selector(viewWillLayoutSubviews)
                            withOptions:0
                             usingBlock:^{
                                        NSLog(@"Controller正在布局!");
                                    }
                                  error:NULL];
}

- (IBAction)openSystemController:(id)sender {
    UIViewController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:imagePickerController animated:YES completion:NULL];
    
    [imagePickerController aspect_hookSelector:@selector(viewWillAppear:)
                                   withOptions:0
                                    usingBlock:^(id<AspectInfo> info, BOOL animated) {
                                                NSLog(@"Controller将要显示！");
                                            }
                                         error:NULL];
    
    // We are interested in being notified when the controller is being dismissed.
    [imagePickerController aspect_hookSelector:@selector(viewWillDisappear:)
                                   withOptions:0
                                    usingBlock:^(id<AspectInfo> info, BOOL animated) {
                                                UIViewController *controller = [info instance];
                                                if (controller.isBeingDismissed || controller.isMovingFromParentViewController) {
                                                    [[[UIAlertView alloc] initWithTitle:@"Popped" message:@"Hello from Aspects" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil] show];
                                                }
                                            }
                                         error:NULL];
    
    // Hooking dealloc is delicate, only AspectPositionBefore will work here.
    [imagePickerController aspect_hookSelector:NSSelectorFromString(@"dealloc")
                                   withOptions:AspectPositionBefore
                                    usingBlock:^(id<AspectInfo> info) {
                                                NSLog(@"Controller将要销毁: %@", [info instance]);
                                            }
                                         error:NULL];
}

@end
